package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.common.interceptor.SysLog;
import com.yqg.education.entity.RoleMenu;
import com.yqg.education.service.IRoleMenuService;
import com.yqg.education.utils.Constants;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * RoleMenuController 前端控制器
 * @author zxf
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/role-menu")
public class RoleMenuController {

    @Resource
    private IRoleMenuService roleMenuService;

    /**
    *  新增
    */
    @PostMapping("/edit")
//    @SysLog("新增角色权限")
    public Result save(@RequestBody RoleMenu roleMenu) {
        return Result.success(roleMenuService.saveOrUpdate(roleMenu));
    }
    /**
     *  修改
     */
    @PostMapping("/updateRoleMenu")
//    @SysLog("修改角色权限")
    @Transactional(rollbackFor = Exception.class)
    public Result updateRoleMenu(@RequestBody List<Long> checkedId){
        if (!roleMenuService.updateRoleMenu(checkedId)) {
            return Result.error(Constants.CODE_500, "修改角色权限失败");
        }
        return Result.success();
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
//    @SysLog("删除角色权限")
    public Result delete(@PathVariable Integer id) {
        RoleMenu roleMenu =roleMenuService.getById(id);
        return Result.success(roleMenuService.saveOrUpdate(roleMenu));
    }
    /**
     * 根据RoleId查询
     */
    @GetMapping("/findAllMenu/{roleId}")
    @ApiOperation("查询用户菜单")
    public Result findAllMenu(@PathVariable Long roleId) {
        //该role所有的menu(父与子都有)
        List<Integer> byRoleId = roleMenuService.getByRoleId(roleId);
        return Result.success(byRoleId);
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
       List<RoleMenu> list=  roleMenuService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleMenuService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(roleMenuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

